<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        #tab-box {
            width: 600px;
            height: 500px;
            border: solid 1px #888;
            border-radius: 5px;
            box-shadow: #888 0 0 3px;
            margin: 50px auto;
            position: relative;
            overflow: hidden;
        }
        #nav{
            height: 50px;
            background-color: rgb(255, 255, 255);
            color: rgb(0, 0, 0);
            display: flex;
            justify-content: space-around;
            border-bottom: 2px solid red;
        }
        #nav p{
            text-align:center;
            flex-grow:1;
            line-height: 50px;
            font-size: 18px;
            font-weight: 600;
            cursor: pointer;
        }
        #nav p.active{
            border-radius: 5px 5px 0 0 ;
            background-color: rgb(255, 0, 0);
            color: white;
            font-weight: 700;
        }
        #content p {
            padding: 50px;
            font-size: 16px;
            width: 100%;
            height: 400px;
            position: absolute;
            left: 0;
            background-color: white;
            display: none;
        }
        #content p:nth-of-type(1) {
            display: block;
        }
    </style>
</head>
<body>
    <div id="tab-box">
        <div id="nav">
            <p class="active">猜你喜欢</p>
            <p>大电器城</p>
            <p>品质生活</p>
        </div>
        <div id="content">
            <p>外星人笔记本</p>
            <p>咖啡机研磨豆</p>
            <p>中国陶瓷艺术品</p>
        </div>
    </div>
    <script>
        // 获取可点击的标题p元素
        var _nav = document.getElementById('nav')
        var _np = _nav.getElementsByTagName('p')
        //  获取展示内容的p元素
        var _content = document.getElementById('content')
        var _cp = _content.getElementsByTagName('p')

        //  给标题p元素添加单击事件
        for(var i = 0; i<_np.length; i++) {
            // 将正在循环的i值，记录到自定义属性中[自定义索引]
            _np[i].index = i
            _np[i].onclick = function() {
                // 处理当前标签激活样式
                for(var m = 0; m < _np.length; m++) {
                    _np[m].className = ""
                }
                this.className = 'active'

                // 让所有的内容p元素隐藏
                for(var j = 0; j<_cp.length; j++) {
                    _cp[j].style.display = "none"
                }
                // 让对应下标的内容p元素显示
                _cp[this.index].style.display = "block"
            }
        }
    </script>
</body>
</html>